import requests
import re
import os

# 目标网页URL  https://sc.chinaz.com/tupian/ribenmeinv.html
# 4K高清：url = 'https://sc.chinaz.com/tupian/220526483493.htm'
url = 'https://sc.chinaz.com/tupian/dongman.html'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36'
}

# 创建保存图片的目录
save_dir = 'downloaded_images'
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# 发送请求获取网页内容
response = requests.get(url=url, headers=headers)
response.encoding = 'utf-8'

# 提取href链接和title
# 4K高清： href_list = re.findall(r"<a class='name' href='(.*?)' title='(.*?)'", response.text)
href_list = re.findall('<a class="name" href="(.*?)" title="(.*?)"', response.text)
print("提取的链接和标题:", href_list)

# 定义基础URL
base_url = 'https://sc.chinaz.com'

# 遍历每个链接
for link_tuple in href_list:
    relative_link = link_tuple[0]
    title = link_tuple[1]

    # 清理标题，使其适合作为文件名
    img_name = re.sub(r'[\\/*?:"<>|]', '', title).strip() + '.jpg'

    # 拼接完整的URL
    full_url = base_url + relative_link
    print(f"正在处理页面: {full_url}")

    # 发送请求获取页面内容
    try:
        html_data = requests.get(url=full_url, headers=headers)
        html_data.encoding = 'utf-8'
        print(f"已获取页面: {full_url}")

        # 提取图片链接
        img_urls = re.findall(r'img src="(.*?)" border', html_data.text)
        print(f"提取的图片链接: {img_urls}")

        # 下载图片
        for img_url in img_urls:
            # 检查图片链接是否为完整URL
            if img_url.startswith('//'):
                img_url = 'https:' + img_url
            elif not img_url.startswith('http'):
                img_url = base_url + img_url

            img_response = requests.get(url=img_url, headers=headers)
            save_path = os.path.join(save_dir, img_name)

            with open(save_path, 'wb') as f:
                f.write(img_response.content)
            print(f"已下载图片: {img_name}")
    except Exception as e:
        print(f"获取页面 {full_url} 时出错: {e}")
